﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;

namespace HaoLab.Library
{
    public class Login
    {
        List<KeyValuePair<string, string>> Params;

        Uri uri;

        const string host = "http://202.204.72.4:8080/reader/";
        HttpWebClient wc;

        bool isLogined = false;

        string userNumber;
        string passwd;

        public Login()
        {
            Params = new List<KeyValuePair<string, string>>();
            Params.Add(new KeyValuePair<string, string>("number", "1102227158"));
            Params.Add(new KeyValuePair<string, string>("passwd", "5200"));
            Params.Add(new KeyValuePair<string, string>("select", "cert_no"));
            Params.Add(new KeyValuePair<string, string>("returnUrl", ""));

            var ss = Observable.Range(0, 2).Repeat(2);

            ss.Subscribe(i => Debug.WriteLine(i), () => Debug.WriteLine("Fin"));

            ss.Select(i => i--);

            uri = new Uri(host + "redr_verify.php");
            wc = new HttpWebClient();

            var ssss = Observable.Create<CookieContainer>(o =>
                {
                    o.OnNext(wc.Cookies);
                    return () => Debug.WriteLine("Creat");
                });

        }


        public void TryLogin()
        {
            var complete = Observable.FromEventPattern<UploadStringCompletedEventArgs>(wc, "UploadStringCompleted").Subscribe(e => Debug.WriteLine(e.EventArgs.Result));

            wc.UploadStringCompleted += new UploadStringCompletedEventHandler((s, e) =>
                {
                    Debug.WriteLine(e.Result);
                    //Renew();
                });
            wc.PostHtmlForm(uri, Params);
        }

        public void Try()
        {
            var complete = Observable.FromEventPattern<UploadStringCompletedEventArgs>(wc, "UploadStringCompleted");



            wc.PostHtmlForm(uri, Params);

        }

        public Subject<CookieContainer> Test()
        {

            Debug.WriteLine(wc.Cookies.Count);
            var complete = Observable.FromEventPattern<UploadStringCompletedEventArgs>(wc, "UploadStringCompleted");

            var x = new Subject<CookieContainer>();
            //x.Subscribe(cc => Debug.WriteLine("!!!!!!!!!!"));
            

            complete.Subscribe(e =>
            {
                Debug.WriteLine("Down");

                x.OnNext(wc.Cookies);
            });

            wc.PostHtmlForm(uri, Params);
            return x;
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Params.Count - 1; i++)
            {
                sb.Append(Params[i].Key + "=" + Params[i].Value + "&");
            }
            sb.Append(Params[Params.Count - 1].Key + "=" + Params[Params.Count - 1].Value);
            return sb.ToString();
        }

        public void Renew()
        {
            DateTime t = new DateTime(1969, 12, 25);
            long iii = (DateTime.Now.ToUniversalTime().Ticks - t.Ticks) / 1000;

            wc.UploadStringCompleted += new UploadStringCompletedEventHandler((s, e) =>
            {
                Debug.WriteLine(e.Result);
            });

            Params = new List<KeyValuePair<string, string>>();
            Params.Add(new KeyValuePair<string, string>("bar_code", "00921206"));
            Params.Add(new KeyValuePair<string, string>("time", iii.ToString()));


            wc.PostHtmlForm(new Uri(host + "ajax_renew.php"), Params);
        }


    }




}
